package com.mapapp.flutter_mapapp.http;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.blankj.utilcode.util.ActivityUtils;
import com.mapapp.flutter_mapapp.activity.LoginActivity;
import com.mapapp.flutter_mapapp.application.Constants;
import com.mapapp.flutter_mapapp.application.MyApplication;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.widget.SimpleLoadDialog;

import rx.Subscriber;

public abstract class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {


    private SimpleLoadDialog dialogHandler;
    private boolean isShowDialog;
    private Context mContext;

    public ProgressSubscriber(Context context, boolean cancelable) {
        this.mContext = context;
        this.isShowDialog = true;
        dialogHandler = new SimpleLoadDialog(context, this, cancelable);
    }

    public ProgressSubscriber(Context context) {
        dismissProgressDialog();
        this.mContext = context;
        this.isShowDialog = true;
        dialogHandler = new SimpleLoadDialog(context, this, true);
    }
//


    @Override
    public void onCompleted() {
        dismissProgressDialog();
    }


    /**
     * 显示Dialog
     */
    public void showProgressDialog() {
        if (dialogHandler != null && isShowDialog) {
//            dialogHandler.obtainMessage(SimpleLoadDialog.SHOW_PROGRESS_DIALOG).sendToTarget();
            dialogHandler.show();
        }
    }

    @Override
    public void onNext(T t) {
        Log.i("onNextTTTT", "TTTTTTT====");
        dismissProgressDialog();
        _onNext(t);
    }

    /**
     * 隐藏Dialog
     */
    private void dismissProgressDialog() {
        if (dialogHandler != null) {
//            dialogHandler.obtainMessage(SimpleLoadDialog.DISMISS_PROGRESS_DIALOG).sendToTarget();
            dialogHandler.dismiss();
//            dialogHandler=null;
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (!isNetworkConnected()) { //这里自行替换判断网络的代码
            _onError("Net work is not available!");
        } else if (e instanceof ApiException) {
//            Log.i("Observableresult1",e.getMessage());
            if (((ApiException) e).getCode() == 4001) {
                try {
                    MyActivityManager.getActivityManager().popAll();
                    Intent intent = new Intent(MyApplication.getInstance(), LoginActivity.class);
                    intent.putExtra(Constants.IS_TOKEN_ERROR, true);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApplication.getInstance().startActivity(intent);
                } catch (Exception e1) {

                }
            } else {
                _onError(e.getMessage());
            }
        } else {
            _onError("无法连接服务器");
        }
        dismissProgressDialog();
    }


    @Override
    public void onCancelProgress() {
        dismissProgressDialog();
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }

    protected abstract void _onNext(T t);

    protected abstract void _onError(String message);

    public boolean isNetworkConnected() {
        if (mContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
